package com.moxiao.yfjh.module.blind.service.memberlabel;

import com.moxiao.yfjh.module.blind.dal.mysql.memberlabel.MemberLabelMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.memberlabel.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberlabel.MemberLabelDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.memberlabel.MemberLabelConvert;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲标签库 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberLabelServiceImpl implements MemberLabelService {

    @Resource
    private MemberLabelMapper memberLabelMapper;

    @Override
    public Long createMemberLabel(MemberLabelCreateReqVO createReqVO) {
        // 插入
        MemberLabelDO memberLabel = MemberLabelConvert.INSTANCE.convert(createReqVO);
        memberLabelMapper.insert(memberLabel);
        // 返回
        return memberLabel.getId();
    }

    @Override
    public void updateMemberLabel(MemberLabelUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberLabelExists(updateReqVO.getId());
        // 更新
        MemberLabelDO updateObj = MemberLabelConvert.INSTANCE.convert(updateReqVO);
        memberLabelMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberLabel(Long id) {
        // 校验存在
        validateMemberLabelExists(id);
        // 删除
        memberLabelMapper.deleteById(id);
    }

    private void validateMemberLabelExists(Long id) {
        if (memberLabelMapper.selectById(id) == null) {
            throw exception(MEMBER_LABEL_NOT_EXISTS);
        }
    }

    @Override
    public MemberLabelDO getMemberLabel(Long id) {
        return memberLabelMapper.selectById(id);
    }

    @Override
    public List<MemberLabelDO> getMemberLabelList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberLabelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberLabelDO> getMemberLabelPage(MemberLabelPageReqVO pageReqVO) {
        return memberLabelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberLabelDO> getMemberLabelList(MemberLabelExportReqVO exportReqVO) {
        return memberLabelMapper.selectList(exportReqVO);
    }

}
